package  
{
	import bulletbehavior.DieBehavior;
	import bulletbehavior.SplitBehavior;
	import bulletbehavior.WaveBehavior;
	import org.flixel.*;
	import org.flixel.FlxSprite;
	import turretbehavior.SpinningTurretBehavior;
	import turretbehavior.TypeCyclingTurretBehavior;
	/**
	 * ...
	 * @author morgan
	 */
	public class Stage1BState extends GameState
	{
		[Embed (source = "../data/tutorial1.png")] private var tut1:Class;
		[Embed (source = "../data/tutorial2.png")] private var tut2:Class;
		[Embed (source = "../data/tutorial3.png")] private var tut3:Class;
		
		[Embed (source = "../data/bosstext.png")] private var img_bossText:Class;
		[Embed (source = "../data/hungerdog_text.png")] private var img_dogText:Class;
		[Embed (source = "../data/food_text.png")] private var img_foodText:Class;
		[Embed (source = "../data/bosslife_text.png")] private var img_bossLife:Class;
		
		[Embed (source = "../data/spacebar_switch.png")] private var img_pressSpace:Class;
		[Embed (source = "../data/pressShift.png")] private var img_pressShift:Class;
		
		public var bossTutText:FlxSprite;
		public var dogTutText:FlxSprite;
		public var foodTutText:FlxSprite;
		public var pressSpace:FlxSprite;
		public var pressShift:FlxSprite;
		public var bossLifeText:FlxSprite;
		
		public var curFood:Bullet = null;
		
		public function Stage1BState() 
		{
			super();
			levelName = "1-1";
			levelIndex = 0;
			chefName = "al dente";
			img_terrain = img_terrain_1;			
		}
		
		override public function create():void 
		{
			super.create();
			
			var col:uint = 0x370000
			
			bossTutText = new FlxSprite(0, 0, img_bossText);
			bossTutText.alpha = 0;
			inGameUI.add(bossTutText);
			bossTutText.color = col;
			
			dogTutText = new FlxSprite(0, 0, img_dogText);
			dogTutText.alpha = 0;
			dogTutText.color = col;
			inGameUI.add(dogTutText);		
			
			
			bossLifeText = new FlxSprite(150, 12, img_bossLife);
			bossLifeText.alpha = 0;
			bossLifeText.color = col;
			inGameUI.add(bossLifeText);			
			
			foodTutText = new FlxSprite(0, 0);
			foodTutText.loadGraphic(img_foodText, true, false, 66, 15);
			foodTutText.alpha = 0;
			foodTutText.color = col;
			inGameUI.add(foodTutText);
			
			pressSpace = new FlxSprite(50, 210);
			pressSpace.loadGraphic(img_pressSpace, true, false, 127, 20);
			pressSpace.frame = 0; pressSpace.alpha = 0;
			pressSpace.color = col;
			inGameUI.add(pressSpace);
			
			pressShift = new FlxSprite( 32, 220, img_pressShift);
			pressShift.alpha = 0;
			pressShift.color = col;
			inGameUI.add(pressShift);			
		}
		
		override public function setupBoss():void 
		{
			boss = new Boss(FlxG.width / 2 - 70, 10, 0);
			boss.velocity.x = 10;
			
			var straight:BulletPattern = new BulletPattern();
			var split3_90:BulletPattern = new BulletPattern();
			split3_90.addBehavior(new SplitBehavior(3, 65, 135, 45, straight, -1));
			split3_90.addBehavior(new DieBehavior());
			
			var split24_360:BulletPattern = new BulletPattern();
			split24_360.addBehavior(new SplitBehavior(16, 80, 0, 360 - (360/16), straight, -1));
			split24_360.addBehavior(new DieBehavior());
			
			var burst32:BulletPattern = new BulletPattern();
			burst32.addBehavior(new SplitBehavior(24, 40, 0, 360 - (360/24), straight, -1), 1);
			burst32.addBehavior(new DieBehavior(), 1);
			
			var tut1Pattern:BossPattern = new BossPattern();
			
			var tut1t:Turret = new Turret(0, 0, straight, 0, 90, 30, 4);
			tut1Pattern.addTurretTime(8, 19, tut1t);
			
			var tut2t:Turret = new Turret(0, 0, straight, 1, 90, 30, 4);
			tut1Pattern.addTurretTime(20, 31, tut2t);
			
			for (var i:int = -11; i < 11; i++)
			{
				var wobbly:BulletPattern = new BulletPattern();
				wobbly.addBehavior(new WaveBehavior(Math.abs(i) / 6 + 1, 0, 3, 0));
				var tut1Turret:Turret = new Turret(i * 15, 0, wobbly, 0, 90, 20, 8);
				tut1Turret.addBehavior(new TypeCyclingTurretBehavior(8, [1, 2, 0]));
				
				
				var tut2Turret:Turret = new Turret(i * 15, 0, wobbly, 0, 90, 20, 8);
				tut2Turret.addBehavior(new TypeCyclingTurretBehavior(8, [0, 1, 2]));
				
				tut1Pattern.addTurretTime(34, 46, tut1Turret);
				tut1Pattern.addTurretTime(56, Infinity, tut2Turret);
				//tut1Pattern.addTurretTime(62, Infinity, tut2Turret);
				
			}
			for (i = -15; i < 15; i++)
			{
				if (i != 0)
				{
					var tutwhat:Turret = new Turret(i * 10, Math.abs(i), straight, Bullet.ROTTEN, 90, 26, 999, false)
					tut1Pattern.addTurretTime(49 + Math.abs(i) / 15, 51, tutwhat);
				}
			}
			
			var tut2Pattern:BossPattern = new BossPattern();
			var tut99Turret:Turret = new Turret(0, 0, split3_90, 65, 90, 0, 2.0);
			tut99Turret.addBehavior(new TypeCyclingTurretBehavior(2.0, [0, 1, 2]));
			
			tut2Pattern.addTurretTime(1, 6, tut99Turret);
			
			
			var tut3Pattern:BossPattern = new BossPattern();
			var tut3Turret1:Turret = new Turret(0, 0, straight, 0, 90, 80, 0.2);
			tut3Turret1.addBehavior(new TypeCyclingTurretBehavior(0.4, [0, 1, 2]));
			tut3Turret1.addBehavior(new SpinningTurretBehavior(120));

			var tut3Turret2:Turret = new Turret(0, 0, straight, 0, 90, 50, 0.1);
			tut3Turret2.addBehavior(new TypeCyclingTurretBehavior(0.2, [0, 1, 2]));
			tut3Turret2.addBehavior(new SpinningTurretBehavior( -360));
			
			//var tut3Turret3:Turret = new Turret(0, 0, split24_360, 0, 90, 50, 1.01);
			//tut3Turret3.addBehavior(new TypeCyclingTurretBehavior(1, [2, 1, 0]));
			
			tut3Pattern.addTurretTime(0, 5, tut3Turret1);
			tut3Pattern.addTurretTime(5, 10, tut3Turret2);
			//tut3Pattern.addTurretTime(11, 12, tut3Turret3);
			
			var tut4Pattern:BossPattern = new BossPattern();
			var tut4Turret1:Turret = new Turret(30, 0, burst32, 0, 90, 30, 4.5);
			var tut4Turret2:Turret = new Turret(0, 0, burst32, 1, 90, 30, 4.5);
			var tut4Turret3:Turret = new Turret(-30, 0, burst32, 2, 90, 30, 4.5);
			tut4Pattern.addTurretTime(0, Number.MAX_VALUE, tut4Turret1);
			tut4Pattern.addTurretTime(1, Number.MAX_VALUE, tut4Turret2);
			tut4Pattern.addTurretTime(2, Number.MAX_VALUE, tut4Turret3);
			
			boss.addState(new BossState(1600, tut1Pattern));
			//boss.addState(new BossState(800, tut2Pattern));
			boss.addState(new BossState(700, tut3Pattern));
			boss.addState(new BossState(1000, tut4Pattern));
			tutorialsEnabled = true;
		}
		
		override public function update():void 
		{
			super.update();
			
			if (boss.states.length == 3)
			{
				growingDisabled = true;
				if (time > 0.5 && time < 7)
				{
					dogTutText.alpha += FlxG.elapsed;
					dogTutText.x = player.x - 84;
					dogTutText.y = player.y - 3;	
					if (time > 3)
					{
						bossTutText.alpha += FlxG.elapsed;
						bossTutText.x = FlxU.bound(boss.x + 8, 20, 120);
						bossTutText.y = boss.y + 85;										
					}
				}
				if (time > 6)
				{
					bossTutText.alpha -= FlxG.elapsed * 2;
					dogTutText.alpha -= FlxG.elapsed * 2;
				}
				if (time > 8 && time < 9999)
				{
					if (curFood)
					{
						foodTutText.frame = (curFood.type == mode) ? 0 : 1;
						foodTutText.alpha += FlxG.elapsed;
						foodTutText.x = curFood.x - 73;
						foodTutText.y = curFood.y - 6;	
						pressShift.alpha = 0;
					}
					if(time > 49 && time < 57) {
						foodTutText.alpha = 0;
						pressShift.alpha = Math.sin((time - 20) * 10 - Math.PI / 2) * 0.4 + 0.6;
					}					
				}
				if (time > 20 && !(time > 49 && time < 57))
				{
					if (curFood && curFood.type == mode) { pressSpace.alpha = 0; }
					else
					{
						pressSpace.alpha = Math.sin((time - 30) * 3 - Math.PI / 2) * 0.4 + 0.6;
						pressSpace.frame = (mode + 1) % 3;
					}
					spaceDisabled = false;
				}
				else
				{
					pressSpace.alpha = 0;
					spaceDisabled = true;
				}
				var lastClosest:FlxSprite = curFood;
				var closest:Number = Infinity;
				for each (var o:Object in bullets.members) {
					var b:Bullet = o as Bullet;
					var dist:Number = Math.pow(b.x - player.x, 2) + Math.pow(b.y - player.y, 2);
					if (b && b.exists && dist < closest && b.y < 220 && b.x > FIELDLEFT + 5 && b.x < FIELDLEFT + FIELDWIDTH - 5)
					{
						curFood = b;
						closest = dist;
					}
				}
				if (!curFood || !curFood.exists)
				{
					foodTutText.alpha = 0.0;
				}
				if (boss.health < 1600 && boss.health > 1200)
				{
					bossLifeText.alpha += FlxG.elapsed;
					bossLifeText.x = (boss.health / 1600) * FIELDWIDTH - 25;
				}
				if (boss.health < 1500)
				{
					bossLifeText.alpha -= FlxG.elapsed * 2;
				}
			}
			else
			{
				foodTutText.alpha = 0; pressSpace.alpha = 0;
				growingDisabled = false;
			}
		}
		
	}

}